package main

import (
	"fmt"
	"os"
	"time"
)

func SimulateTimeout() {
	ch := make(chan int)

	go func() {
		for {
			select {
			case num := <-ch:
				fmt.Println("num = ", num)
			case <-time.After(10 * time.Second):
				fmt.Println("超时, 将退出程序")
				os.Exit(0)
			}
		}

	}()

	for i := 0; i < 5; i++ {
		ch <- i
	}
	var n int
	for {
		fmt.Println("请输入一个整数，超过10秒未输入将超时退出程序")
		_, _ = fmt.Scan(&n)
		if n == 0 {
			break
		}
		ch <- n
	}

	fmt.Println("程序结束")
}
